package cn.autowired.exam.controller;

import cn.autowired.exam.base.CommonResponse;
import cn.autowired.exam.entity.Teacher;
import cn.autowired.exam.service.TeacherService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.autowired.exam.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.autowired.exam.service.TeacherFreeTimeService;

import cn.autowired.exam.entity.TeacherFreeTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

/**
 *
 *   @description : TeacherFreeTime 控制器
 *   ---------------------------------
 *      @author cq
 *   @since 2018-05-25
 */
@RestController
@RequestMapping("/teacherFreeTime")
public class TeacherFreeTimeController extends AbstractController {
private final Logger logger=LoggerFactory.getLogger(TeacherFreeTimeController.class);

    @Autowired
    public TeacherFreeTimeService teacherFreeTimeService;

    @Autowired
    public TeacherService teacherService;

    /**
     *  根据老师id和指定时间来查找那一天的空闲时间段
     *
     *
     *
     *
     * @param teacherId
     * @param freeDate
     * @return
     */
    @RequestMapping("/selectFreeTimeOnDate")
    public ResponseEntity selectFreeTimeOnDate(Integer teacherId,String freeDate){
        TeacherFreeTime teacherFreeTime = teacherFreeTimeService.selectByTeacherIdAndFreeDate(teacherId, freeDate);
        if (teacherFreeTime==null){
            return ResponseEntity.ok(CommonResponse.error());
        }
        return ResponseEntity.ok(CommonResponse.ok(teacherFreeTime));
    }

    /**
     * 批量初始化老师的空闲时间,根据开始和结束日期(yyyy-MM-dd)
     * @param teacherId
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @RequestMapping("/insertFreeTimeByBatch")
    public ResponseEntity insertFreeTimeByBatch(Integer teacherId, String startTime, String endTime) throws  ParseException {
        Calendar cal = Calendar.getInstance();
        String[] startTimeList = startTime.split("-");
        cal.set(Integer.parseInt(startTimeList[0]), Integer.parseInt(startTimeList[1])-1, Integer.parseInt(startTimeList[2]));
        Teacher teacher = teacherService.selectById(teacherId);
        String freeTime = teacher.getFreeTime();
        String[] freeTimeList = freeTime.split(",");

            for(int i=0;i<365;i++){
                cal.add(Calendar.DATE, 1);
                for (String week : freeTimeList) {
                    if ((cal.get(Calendar.DAY_OF_WEEK)) == Integer.parseInt(week)) {
                        TeacherFreeTime teacherFreeTime = new TeacherFreeTime();
                        teacherFreeTime.setTeacherId(teacherId);
                        teacherFreeTime.setFreeDate(cal.getTime());
                        teacherFreeTime.insertOrUpdate();
                    }
                }
                if(DateFormatUtils.format(cal.getTime(),"yyyy-MM-dd").equals(endTime)){
                    break;
                }
            }
        return ResponseEntity.ok(CommonResponse.ok());
    }


    /**
     *  预约空闲时间
     * @param teacherId
     * @param freeDate
     * @param freeTime
     * @return
     */

    @RequestMapping("/setFreeTime")
    public ResponseEntity setFreeTimeByTeacherIdOnFreeDate(Integer teacherId,
                                                           String freeDate,
                                                           String  freeTime){
        TeacherFreeTime teacherFreeTime = teacherFreeTimeService.selectByTeacherIdAndFreeDate(teacherId,freeDate);
        if (teacherFreeTime != null) {
            switch (freeTime){
                case "time8to9":
                    if (teacherFreeTime.getTime8to9()==1){
                        return  ResponseEntity.ok(CommonResponse.error());
                    }
                    teacherFreeTime.setTime8to9(1);
                    break;
                case "time9to10":
                    if (teacherFreeTime.getTime9to10()==1){
                        return  ResponseEntity.ok(CommonResponse.error());
                    }
                    teacherFreeTime.setTime9to10(1);
                    break;
                case "time10to11":
                    if (teacherFreeTime.getTime10to11()==1){
                        return  ResponseEntity.ok(CommonResponse.error());
                    }
                    teacherFreeTime.setTime10to11(1);
                    break;
                case "time11to12":
                    if (teacherFreeTime.getTime11to12()==1){
                        return  ResponseEntity.ok(CommonResponse.error());
                    }
                    teacherFreeTime.setTime11to12(1);
                    break;
                case "time14to15":
                    if (teacherFreeTime.getTime14to15()==1){
                        return  ResponseEntity.ok(CommonResponse.error());
                    }
                    teacherFreeTime.setTime14to15(1);
                    break;
                case "time15to16":
                    if (teacherFreeTime.getTime15to16()==1){
                        return  ResponseEntity.ok(CommonResponse.error());
                    }
                    teacherFreeTime.setTime15to16(1);
                    break;
                case "time16to17":
                    if (teacherFreeTime.getTime16to17()==1){
                        return  ResponseEntity.ok(CommonResponse.error());
                    }
                    teacherFreeTime.setTime16to17(1);
                    break;
                    default:return ResponseEntity.ok(CommonResponse.error());
            }
            teacherFreeTime.updateById();
        }else {
            return ResponseEntity.ok(CommonResponse.error());
        }
        return ResponseEntity.ok(CommonResponse.ok());
    }
}